package org.hishenframework.core.data.jdbc.template;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.persistence.Column;


/**
 * 数据库工具类，支持jpa2.0
 * @author didi
 *
 */
public class JpaJdbcUtils {
	
//	public static void put(PreparedStatement pstmt, Object... values) throws SQLException {
//		if (values != null) {
//			int parameterIndex = 1;
//			for (Object value : values) {
//				pstmt.setObject(parameterIndex, value);
//				parameterIndex++;
//			}
//		}
//		List<String> s = JdbcUtil.get();
//	}
	
	/**
	 * 将结果集中的一行转换为对象
	 * @param rs 结果集
	 * @param clazz 类
	 * @return
	 */
	public static <T> T getObject(ResultSet rs, Class<?> clazz) {
		try {
			T t = (T) clazz.newInstance();
			ResultSetMetaData metaData = rs.getMetaData();
			for (int i = 1; i <= metaData.getColumnCount(); i++) {
				String columnName = metaData.getColumnName(i);
				Object value = rs.getObject(columnName);
				setValue(t, columnName, value);
			}
			return t;
		} catch (InstantiationException | IllegalAccessException | SQLException |  InvocationTargetException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 设置对象的属性的值
	 * @param o 对象
	 * @param columnName 数据库列名
	 * @param value 值
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 */
	public static void setValue(Object o, String columnName, Object value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Class<?> clazz = o.getClass();
		String fieldName = underlinToUpperCase(columnName);
		String methodName = null;
		//匹配@Column 中的name
		for (Field field : clazz.getDeclaredFields()) {
			Column columnAnn = field.getAnnotation(Column.class);
			if (columnAnn != null && columnAnn.name().equals(columnName)) {
				methodName = "set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
				break;
			}
		}
		//没有匹配中@Column，则使用转换后的列名
		if (methodName == null) {
			methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
		}
		for (Method method : clazz.getMethods()) {
			if (method.getName().equals(methodName) && method.getGenericParameterTypes().length == 1) {
				method.invoke(o, value);
				break;
			}
		}
	}
	
	/**
	 * 下划线转大写
	 * @param text
	 * @return
	 */
	public static String underlinToUpperCase(String text) {
		Pattern pattern = Pattern.compile("(_[a-z])");
		Matcher matcher = pattern.matcher(text);
		while (matcher.find()) {
			text = text.replace(matcher.group(1), matcher.group(1).substring(1).toUpperCase());
		}
		return text.substring(0, 1).toLowerCase() + text.substring(1);
	}

	/**
	 * 大写转下划线
	 * @param text
	 * @return
	 */
	public static String upperCaseToUnderline(String text) {
		Pattern pattern = Pattern.compile("([A-Z])");
		Matcher matcher = pattern.matcher(text);
		while (matcher.find()) {
			text = text.replace(matcher.group(1), "_" + matcher.group(1).toLowerCase());
		}
		if(text.substring(0, 1).equals("_")) {
			text = text.substring(1);
		}
		return text;
	}
}
